package com.javadaemon.Tinderbox.loader;

import java.applet.AppletContext;
import java.applet.AppletStub;
import java.net.URL;
import java.net.URLClassLoader;
import java.applet.Applet;

import com.javadaemon.Tinderbox.Tinderbox;

/**
 * This file is part of Tinderbox.
 *
 * Tinderbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ponix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tinderbox.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This class loads the runescape applet.
 * @author Mads Horndrup
 */
public class GameAppletLoader implements AppletStub {

    private Applet applet;
    private Tinderbox parent;

    public GameAppletLoader(Tinderbox parent) {
        this.parent = parent;
        try {
            URLClassLoader localURLClassLoader = new URLClassLoader(new URL[]{new URL(Tinderbox.parseClientLoader())});
            applet = (Applet) localURLClassLoader.loadClass("loader").newInstance();
            applet.setStub(this);
            applet.setVisible(true);
            applet.init();
            applet.start();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public Applet getApplet() {
        return applet;
    }

    @Override
    public boolean isActive() {
        return applet.isActive();
    }

    @Override
    public void appletResize(int arg0, int arg1) {
    }

    @Override
    public AppletContext getAppletContext() {
        return null;
    }

    @Override
    public URL getDocumentBase() {
        return getCodeBase();
    }

    @Override
    public URL getCodeBase() {
        try {
            return new URL(Tinderbox.URL);
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    public String getParameter(String req) {
        return parent.props.get(req);
    }
}
